package com.qf.controller;

import com.qf.entity.SysUser;
import com.qf.service.SysUserService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.UUID;

/**
 * --- 机魂大悦 ---
 *
 * @author L
 * @date 2024/7/16
 * @desc
 */
@CrossOrigin
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/login")
    public R login(@RequestBody SysUser sysUser, HttpSession session) {
        System.out.println("SysUserController.login,接收数据" + sysUser);
        SysUser login = sysUserService.login(sysUser);
        if (login != null) {
            session.setAttribute("userInfo", login);
            // 假token，给前端
            HashMap<String, String> map = new HashMap<>();
            map.put("token", UUID.randomUUID().toString());
            return R.ok(map);
        }
        return R.fail();
    }

    @GetMapping("/info")
    public R getUserInfo(HttpSession session) {
        SysUser info = (SysUser) session.getAttribute("userInfo");
        System.out.println("SysUserController.getUserInfo" + info);
        return R.ok(info);
    }
    @PostMapping("/logout")
    public R logout (HttpSession session){
        session.invalidate();
        return R.ok();
    }


}
